﻿#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void menu()
{
	printf("-----------------\n");
	printf("---- 1.start ----\n");
	printf("---- 0.exit  ----\n");
	printf("-----------------\n");

}

void game()
{
	//设计俩个数组存放信息
	char mine[ROWS][COLS];
	char show[ROWS][COLS];

	//初始化棋盘
	init_board(mine, '0');
	init_board(show, '*');

	//display_board(mine);
	//display_board(show);

	//布置雷
	set_mine(mine);
	//display_board(mine);
	display_board(show);
	//找雷
	find_mine(mine, show);
}

void init_board(char board[ROWS][COLS], char num)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < ROWS; i++)
	{
		for (j = 0; j < COLS; j++)
		{
			board[i][j] = num;
		}
	}
}

void display_board(char board[ROWS][COLS])
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= COL; i++)
	{
		printf("%d ", i);
	}
	printf("\n");

	for (i = 1; i <= ROW; i++)
	{
		printf("%d ", i);

		for (j = 1; j <= COL; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void set_mine(char mine[ROWS][COLS])
{
	int count = EASY_COUNT;

	while (count)
	{
		int x = rand() % ROW + 1;
		int y = rand() % COL + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

find_mine(char mine[ROWS][COLS], char show[ROWS][COLS])
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < ROW * COL - EASY_COUNT)
	{
		printf("请输入要排查雷的坐->");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
		{
			if (show[x][y] == '*')
			{
				if (mine[x][y] == '1')
				{
					printf("你被炸死啦\n");
					display_board(mine, ROW, COL);
					break;
				}
				else
				{
					int count = get_mine_count(mine, x, y);
					show[x][y] = count + '0';
					display_board(show);
					win++;
				}
			}
			else
			{
				printf("坐标已被排查，请重新选择\n");
			}
		}
		else
		{
			printf("坐标非法，请重新输入\n");
		}
	}
	if (win == ROW * COL - EASY_COUNT)
	{
		printf("恭喜你，排雷成功！\n");
		display_board(mine);
	}
}

int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	return (mine[x - 1][y - 1] +
		mine[x][y - 1] +
		mine[x + 1][y - 1] +
		mine[x - 1][y] +
		mine[x + 1][y] +
		mine[x - 1][y + 1] +
		mine[x][y + 1] +
		mine[x + 1][y + 1] - 8 * '0'
		);
}